package com.seu.githubcn.controller;
import com.seu.githubcn.config.Result;
import lombok.Data;
import org.eclipse.jgit.api.Git;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping()
public class BranchController {


    @Value("${githubcn.base-path}")
    String BASE;

    @PostMapping("{userName}/{repoName}/branch/new")
    public Result<?> newBranch(@PathVariable("userName")String userName,
                               @PathVariable("repoName")String repoName,
                               @RequestBody Map<String,String> map) {
        var baseBranch = map.get("baseBranch");
        var targetBranch = map.get("targetBranch");
        var localPath = BASE.formatted(userName,repoName);
        try {
            Git git = Git.open(new File(localPath));
            git.branchCreate().setName(targetBranch).setStartPoint(baseBranch).call();
        } catch (Exception e) {
            return new Result<>(400,null,"创建失败！");
        }
        return new Result<>(200,null,"创建成功！");
    }

    @PostMapping("{userName}/{repoName}/branch/delete")
    public Result<?> deleteBranch(@PathVariable("userName")String userName,
                               @PathVariable("repoName")String repoName,
                               @RequestBody Map<String,String> map) {
        var branchName = map.get("branchName");
        var localPath = BASE.formatted(userName,repoName);
        try {
            Git git = Git.open(new File(localPath));
            git.branchDelete().setForce(true).setBranchNames(branchName).call();
        } catch (Exception e) {
            return new Result<>(400,null,"删除失败！");
        }
        return new Result<>(200,null,"删除成功！");
    }

    @GetMapping("{userName}/{repoName}/branches")
    public Result<?> branchList(@PathVariable("userName")String userName,
                                  @PathVariable("repoName")String repoName) {
        var localPath = BASE.formatted(userName,repoName);
        try {
            Git git = Git.open(new File(localPath));
            var refs = git.branchList().call();
            List<BranchInfo> branchInfoList = new ArrayList<>();

            for(var ref :refs){
                BranchInfo branchInfo = new BranchInfo();
                branchInfo.setBranchName(ref.getName().split("/")[2]);
                branchInfoList.add(branchInfo);
            }
            return new Result<>(200,branchInfoList,"查询成功！");
        } catch (Exception e) {
            return new Result<>(400,null,"查询失败！");
        }
    }
    @GetMapping("releases/new/info")
    public Result<?> branchList_newRelease(
            @RequestParam(defaultValue = "")String userName,
            @RequestParam(defaultValue = "")String repoName) {
        var localPath = BASE.formatted(userName,repoName);
        try {
            Git git = Git.open(new File(localPath));
            var refs = git.branchList().call();
            List<BranchInfo> branchInfoList = new ArrayList<>();

            for(var ref :refs){
                BranchInfo branchInfo = new BranchInfo();
                branchInfo.setBranchName(ref.getName().split("/")[2]);
                branchInfoList.add(branchInfo);
            }
            return new Result<>(200,branchInfoList,"查询成功！");
        } catch (Exception e) {
            return new Result<>(400,null,"查询失败！");
        }
    }

    @Data
    public static class BranchInfo{
        private String branchName;
    }
}
